package demo2lambda;

import java.security.Key;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:函数式接口：当一个接口当中 只有一个抽象方法的时候
 * User: 牛孟俊
 * Date: 2023-11-21
 * Time: 17:45
 */


//无返回值无参数

    @FunctionalInterface//这个注解表示要求下面这个接口要按照函数式接口来定义，否则会报错
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
        void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
        void test(int a,int b);
}

//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
        int test();
}

//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
        int test(int a);
}

//有返回值多个参数
@FunctionalInterface
interface MoreParameterReturn {
        int test(int a,int b);
}


public class Test {
    
    
    public static void main5(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("anc",3);
        map.put("msd",1);
        map.put("N",4);

        /*map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key " + s + " value " + integer);
            }
        });*/

        map.forEach((k,v) -> System.out.println("key " + k + "value" + v));
    }
    public static void main4(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("c");
        list.add("b");
        /*list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });*/

        list.forEach(s -> System.out.println(s));
        System.out.println("===================");
        /*list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });*/

        //Lambda表达式
        list.sort((o1,o2) -> o1.compareTo(o2));
        list.forEach(s -> System.out.println(s));


    }
    public static void main3(String[] args) {
        int a = 10;
        NoParameterNoReturn noparameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("a=" + a);
            }
        };
        noparameterNoReturn.test();
    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn = (a) -> {
            System.out.println(a);
        };

        oneParameterNoReturn.test(10);
        System.out.println("==========");

        MoreParameterNoReturn moreParameterNoReturn = (a,b) -> {
            System.out.println(a+b);
        };

        moreParameterNoReturn.test(10,20);

        System.out.println("=======没有参数有返回值==========");

        //NoParameterReturn noParameterReturn = () -> {return 10;};
        NoParameterReturn noParameterReturn = () -> 10;
        int ret = noParameterReturn.test();
        System.out.println(ret);
        System.out.println("===========有返回值一个参数==========");

        OneParameterReturn oneParameterReturn = (a) -> {
            return a*2;
        };

        int ret2 = oneParameterReturn.test(12);
        System.out.println(ret2);

        System.out.println("======有返回值多个参数=========");

        MoreParameterReturn moreParameterReturn = (a,b) -> a*b;
        int ret3 = moreParameterReturn.test(10,20);
        System.out.println(ret3);

    }
    public static void main1(String[] args) {
        NoParameterNoReturn noparameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("1234");
            }
        };
        noparameterNoReturn.test();
        System.out.println("=====================");
        //上面是没有lambda表达式的写法

        NoParameterNoReturn noParameterNoReturn2 = () -> System.out.println("1234");
        //() -> System.out.println("1234")相当于重写了test()方法

        noParameterNoReturn2.test();
    }
}
